<?php

namespace yphp;

class Runtime extends Instance
{

    // ----------------------------------------------------------------------
    //  日志容量(b)
    // ----------------------------------------------------------------------

    private $size;

    // ----------------------------------------------------------------------
    //  当前时间：日期、时、分、秒
    // ----------------------------------------------------------------------

    private $date;

    private $hour;

    private $minute;

    private $second;

    //+----------------------------------------------------------------------
    //  构造方法
    //+----------------------------------------------------------------------

    function construct($option)
    {
        $this->size = Config::runtime($option);
        $this->size > 0 && list($this->date, $this->hour, $this->minute, $this->second) = explode(' ', date('Ymd H i s'));
    }

    //+----------------------------------------------------------------------
    //  访问日志
    //+----------------------------------------------------------------------

    static function action()
    {
        $obj = self::instance('log');
        if ($obj->size > 0) {
            $key = $obj->minute * 60 + $obj->second;
            if (is_file($file = CACHE_LOG_PATH . $obj->date . DS . $obj->hour . DS . Safe::en(Request::instance()->path()) . '.json')) {
                $action = json_decode(file_get_contents($file), true);
                $action['count']++;
                key_exists($key, $action['details']) ? $action['details'][$key]++ : $action['details'][$key] = 1;
            } else {
                $action = ['count' => 1, 'details' => [$key => 1]];
            }
            DealFile::writeFile($file, json_encode($action), true);
            self::limit(CACHE_LOG_PATH, $obj->size);
        }
    }

    // ----------------------------------------------------------------------
    //  错误日志
    // ----------------------------------------------------------------------

    static function error($info)
    {
        $obj = self::instance('error');
        if ($obj->size > 0) {
            DealFile::append(CACHE_ERROR_PATH . $obj->date . DS . $obj->hour . '.log', $obj->minute . ':' . $obj->second . ' ' . $info . PHP_EOL);
            self::limit(CACHE_ERROR_PATH, $obj->size);
        }
    }

    // ----------------------------------------------------------------------
    //  SQL日志
    // ----------------------------------------------------------------------

    static function sql($info)
    {
        $obj = self::instance('sql');
        if ($obj->size > 0) {
            DealFile::append(CACHE_SQL_PATH . $obj->date . DS . $obj->hour . '.log', $obj->minute . ':' . $obj->second . ' ' . $info . PHP_EOL);
            self::limit(CACHE_SQL_PATH, $obj->size);
        }
    }

    // ----------------------------------------------------------------------
    //  保持日志存储上限[删除旧版]
    // ----------------------------------------------------------------------

    private static function limit($dir, $limit)
    {
        $list = DealFile::lists(rtrim($dir, DS));
        $size = $list['size'];
        $down = $list['down'];
        $i = 0;
        while ($size > $limit) {
            DealFile::delDir($down[$i]['path'], true);
            $size -= $down[$i]['size'];
            $i++;
        }
    }

}